//
//  CustomCalloutView.m
//  Meroad
//
//  Created by StariMac1 on 2019/3/20.
//  Copyright © 2019 郭孝通. All rights reserved.
//

#import "CustomCalloutView.h"
#import "ToiletModel.h"
#import "AppDelegate.h"

#define xInterval 10
#define yInterval 10
#define basicHeight 16

@interface CustomCalloutView ()

@property (nonatomic, weak)UserModel *userModel;

@property (nonatomic, weak)ToiletModel *toiletModel;

@property (nonatomic,strong) UILabel *uuidLabel;
@property (nonatomic,strong) UILabel *addressLabel;
@property (nonatomic,strong) UILabel *spareHolesNumberLabel;
@property (nonatomic,strong) UILabel *allHolesNumberLabel;
@property (nonatomic,strong) UILabel *hygieneLevelLabel;
@property (nonatomic,strong) UILabel *userEvaluationLabel;
@property (nonatomic,strong) UILabel *sortLabel;
@property (nonatomic,strong) UILabel *sortResultLabel;
@property (nonatomic,strong) UILabel *distantLabel;
@property (nonatomic, strong) UILabel *leftPaperLabel;

@end

@implementation CustomCalloutView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor clearColor];
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews
{
    //控件布局在此实现
    
    if([self.userModel.userType isEqualToString:@"管理员"]){
        [self initSuperSubviews];
    }
    else{
        [self initNormalSubViews];
    }
    
    [self.userModel addObserver:self forKeyPath:@"userType" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];
}

- (void)initNormalSubViews{
    //地址
    self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(xInterval , yInterval  , self.frame.size.width - 2 * xInterval, basicHeight * 3)];
    self.addressLabel.numberOfLines = 3;
    self.addressLabel.adjustsFontSizeToFitWidth = YES;
    [self.addressLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
    self.addressLabel.textColor = [UIColor blackColor];
    self.addressLabel.text = self.toiletModel.address;
    [self addSubview:self.addressLabel];
    
    //排序
    UILabel *sortLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width / 2 + xInterval, yInterval + basicHeight * 3.5, 50 * xInterval, basicHeight)];
    [sortLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
    sortLabel.textColor = [UIColor blackColor];
    sortLabel.text = @"优先级推荐：";
    [self addSubview:sortLabel];
    
    self.sortResultLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width / 2 + xInterval * 11, yInterval + basicHeight * 2.9, 5 * xInterval , basicHeight * 2)];
    [self.sortResultLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:25]];
    self.sortResultLabel.textColor = [UIColor blackColor];
    self.sortResultLabel.text = [NSString stringWithFormat:@"%ld",self.toiletModel.sortResult] ;
    [self addSubview:self.sortResultLabel];
    
    //距离
    self.distantLabel = [[UILabel alloc] initWithFrame:CGRectMake(xInterval, yInterval + basicHeight * 3.5, self.frame.size.width - 4 * xInterval, basicHeight)];
    [self.distantLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
    self.distantLabel.textColor = [UIColor blackColor];
    self.distantLabel.text = [NSString stringWithFormat:@"距离约 %d 米",(int)self.toiletModel.distant] ;
    [self addSubview:self.distantLabel];
    
    //分割线
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(xInterval, yInterval + basicHeight * 4.9  , self.frame.size.width - 4 * xInterval, basicHeight / 7)];
    [line setBackgroundColor:[UIColor lightGrayColor]];
    [self addSubview:line];
    
    // 空蹲位
    self.spareHolesNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(xInterval, yInterval + basicHeight * 5.3 + basicHeight / 7, (self.frame.size.width - 2 * xInterval) / 3, basicHeight)];
    [self.spareHolesNumberLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
    self.spareHolesNumberLabel.textColor = [UIColor blackColor];
    self.spareHolesNumberLabel.text = [NSString stringWithFormat:@"空余蹲位数：%ld",self.toiletModel.spareHolesNumber] ;
    [self addSubview:self.spareHolesNumberLabel];
    
    // 卫生等级
    self.hygieneLevelLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width / 2 + xInterval, yInterval + basicHeight * 5.3 + basicHeight / 7, (self.frame.size.width - 2 * xInterval) / 3, basicHeight)];
    [self.hygieneLevelLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
    self.hygieneLevelLabel.textColor = [UIColor blackColor];
    self.hygieneLevelLabel.text = [NSString stringWithFormat:@"卫生等级：%ld",self.toiletModel.hygieneLevel];
    [self addSubview:self.hygieneLevelLabel];
}

- (void)initSuperSubviews{
    //地址
    self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(xInterval , yInterval  , self.frame.size.width - 2 * xInterval, basicHeight * 3)];
    self.addressLabel.numberOfLines = 3;
    self.addressLabel.adjustsFontSizeToFitWidth = YES;
    [self.addressLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
    self.addressLabel.textColor = [UIColor blackColor];
    self.addressLabel.text = self.toiletModel.address;
    [self addSubview:self.addressLabel];
    
    // uuid
    self.uuidLabel = [[UILabel alloc] initWithFrame:CGRectMake(xInterval, yInterval + basicHeight * 3.5  , self.frame.size.width, basicHeight)];
    [self.uuidLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
    self.uuidLabel.textColor = [UIColor blackColor];
    self.uuidLabel.text = [NSString stringWithFormat:@"UUID：%@",self.toiletModel.uuid] ;
    [self addSubview:self.uuidLabel];
    
    //        //距离
    //        self.distantLabel = [[UILabel alloc] initWithFrame:CGRectMake(xInterval, yInterval + basicHeight * 4.6, self.frame.size.width - 4 * xInterval, basicHeight)];
    //        [self.distantLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
    //        self.distantLabel.textColor = [UIColor blackColor];
    //        self.distantLabel.text = [NSString stringWithFormat:@"距离约 %d 米",(int)self.toiletModel.distant] ;
    //        [self addSubview:self.distantLabel];
    
    //分割线
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(xInterval, yInterval + basicHeight * 4.9, self.frame.size.width - 4 * xInterval, basicHeight / 7)];
    [line setBackgroundColor:[UIColor lightGrayColor]];
    [self addSubview:line];
    
    // 卫生等级
    self.hygieneLevelLabel = [[UILabel alloc] initWithFrame:CGRectMake(xInterval, yInterval + basicHeight * 5.3 + basicHeight / 7, (self.frame.size.width - 2 * xInterval) / 3, basicHeight)];
    [self.hygieneLevelLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
    self.hygieneLevelLabel.textColor = [UIColor blackColor];
    self.hygieneLevelLabel.text = [NSString stringWithFormat:@"卫生等级：%ld",self.toiletModel.hygieneLevel];
    [self addSubview:self.hygieneLevelLabel];
    
    // 用户评级
    self.userEvaluationLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width / 2 + xInterval, yInterval + basicHeight * 5.3 + basicHeight / 7, (self.frame.size.width - 2 * xInterval) / 3, basicHeight)];
    [self.userEvaluationLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
    self.userEvaluationLabel.textColor = [UIColor blackColor];
    self.userEvaluationLabel.text = [NSString stringWithFormat:@"用户评级：%ld",self.toiletModel.userEvaluation];
    [self addSubview:self.userEvaluationLabel];
    
    //剩余纸量
    self.leftPaperLabel = [[UILabel alloc] initWithFrame:CGRectMake(xInterval, yInterval + basicHeight * 6.7 + 2 * basicHeight / 7, (self.frame.size.width - 2 * xInterval), basicHeight)];
    [self.leftPaperLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
    self.leftPaperLabel.textColor = [UIColor blackColor];
    switch (self.toiletModel.leftPaper) {
        case 0:
            self.leftPaperLabel.text = [NSString stringWithFormat:@"剩余纸量：不足，需补充！"];
            [self.leftPaperLabel setTextColor:[UIColor redColor]];
            break;
        case 1:
            self.leftPaperLabel.text = [NSString stringWithFormat:@"剩余纸量：少"];
            [self.leftPaperLabel setTextColor:[UIColor orangeColor]];
            break;
        case 2:
            self.leftPaperLabel.text = [NSString stringWithFormat:@"剩余纸量：中"];
            [self.leftPaperLabel setTextColor:[UIColor blueColor]];
            break;
        case 3:
            self.leftPaperLabel.text = [NSString stringWithFormat:@"剩余纸量：多"];
            [self.leftPaperLabel setTextColor:[UIColor blackColor]];
            break;
        default:
            self.leftPaperLabel.text = [NSString stringWithFormat:@"剩余纸量：检测失败！"];
            [self.leftPaperLabel setTextColor:[UIColor redColor]];
            break;
    }
    
    [self addSubview:self.leftPaperLabel];
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([self.userModel.userType isEqualToString: @"管理员"]) {
        for (UIView *view in self.subviews) {
            [view removeFromSuperview];
        }
        [self initSuperSubviews];
    } else {
        for (UIView *view in self.subviews) {
            [view removeFromSuperview];
        }
        [self initNormalSubViews];
    }
}

- (void)reloadCalloutView{
    self.addressLabel.text = self.toiletModel.address;
    if (self.spareHolesNumberLabel) {
        self.spareHolesNumberLabel.text = [NSString stringWithFormat:@"空余蹲位数：%ld",self.toiletModel.spareHolesNumber] ;
    }
    if (self.allHolesNumberLabel) {
        self.allHolesNumberLabel.text = [NSString stringWithFormat:@"总蹲位数：%ld",self.toiletModel.allHolesNumber];
    }
    if (self.hygieneLevelLabel) {
        self.hygieneLevelLabel.text = [NSString stringWithFormat:@"卫生等级：%ld",self.toiletModel.hygieneLevel];
    }
    if (self.userEvaluationLabel) {
        self.userEvaluationLabel.text = [NSString stringWithFormat:@"用户评级：%ld",self.toiletModel.userEvaluation];
    }
    if(self.leftPaperLabel){
        switch (self.toiletModel.leftPaper) {
            case 0:
                self.leftPaperLabel.text = [NSString stringWithFormat:@"剩余纸量：不足，需补充！"];
                [self.leftPaperLabel setTextColor:[UIColor redColor]];
                break;
            case 1:
                self.leftPaperLabel.text = [NSString stringWithFormat:@"剩余纸量：少"];
                [self.leftPaperLabel setTextColor:[UIColor orangeColor]];\
                break;
            case 2:
                self.leftPaperLabel.text = [NSString stringWithFormat:@"剩余纸量：中"];
                [self.leftPaperLabel setTextColor:[UIColor blueColor]];
                break;
            case 3:
                self.leftPaperLabel.text = [NSString stringWithFormat:@"剩余纸量：多"];
                [self.leftPaperLabel setTextColor:[UIColor blackColor]];
                break;
            default:
                self.leftPaperLabel.text = [NSString stringWithFormat:@"剩余纸量：检测失败！"];
                [self.leftPaperLabel setTextColor:[UIColor redColor]];
                break;
        }
    }
    if(self.uuidLabel){
        self.uuidLabel.text = [NSString stringWithFormat:@"UUID: %@",self.toiletModel.uuid] ;
    }
    if(self.sortResultLabel){
        self.sortResultLabel.text = [NSString stringWithFormat:@"%ld",self.toiletModel.sortResult] ;
    }
    if (self.distantLabel) {
        self.distantLabel.text = [NSString stringWithFormat:@"距离约 %d 米",(int)self.toiletModel.distant] ;
    }
    
}


- (void)setToiletModel:(ToiletModel *)model
{
    _toiletModel = model;
}

//绘制弹出气泡的背景
- (void)drawRect:(CGRect)rect {
    // Drawing code
    [self drawInContext:UIGraphicsGetCurrentContext()];
    
    self.layer.shadowColor = [[UIColor whiteColor] CGColor];
    self.layer.shadowOpacity = 1.0;
    self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
}

- (void)drawInContext:(CGContextRef)context
{
    CGContextSetLineWidth(context, 2.0);
    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5].CGColor);
    [self getDrawPath:context];
    CGContextFillPath(context);
}

- (void)getDrawPath:(CGContextRef)context
{
    CGRect rrect = self.bounds;
    CGFloat radius = 10.0;
    CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect), maxy = CGRectGetMaxY(rrect);
    
    CGContextMoveToPoint(context, midx, maxy);
    CGContextAddLineToPoint(context, minx, maxy);
    CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, maxy, radius);
    CGContextAddLineToPoint(context, maxx, maxy);
    CGContextAddLineToPoint(context, midx, maxy);
    CGContextClosePath(context);
    
}

- (void)hide{
    AppDelegate *_rootDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    CustomTabBar *tabbar = (CustomTabBar *)[_rootDelegate getTabbar];
    [_rootDelegate showTabBar:tabbar];
    
    if(self.frame.origin.y != SCREEN_HEIGHT){
        [UIView animateWithDuration:0.3 animations:^{
            [self setFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, ([_rootDelegate.userModel.userType isEqualToString:@"管理员"] ? 120 : 100)  + myTabbarHeight)];
            
        }   completion:^(BOOL finished) {
            [self removeFromSuperview];
            
        }];
    }
}
- (void)show{
    if(self.frame.origin.y == SCREEN_HEIGHT){
        AppDelegate *_rootDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        
        [_rootDelegate.window addSubview:self];
        
        [UIView animateWithDuration:0.3 animations:^{
            [self setFrame:CGRectMake(0, SCREEN_HEIGHT - ([_rootDelegate.userModel.userType isEqualToString:@"管理员"] ? 120 : 100) - myTabbarHeight, SCREEN_WIDTH, ([_rootDelegate.userModel.userType isEqualToString:@"管理员"] ? 120 : 100) + myTabbarHeight)];
            
        }   completion:^(BOOL finished) {
            CustomTabBar *tabbar = (CustomTabBar *)[_rootDelegate getTabbar];
            [_rootDelegate hideTabbar:tabbar];
        }];
    }
}

#pragma mark 重写get方法(懒加载)

- (UserModel *)userModel{
    if (_userModel == nil) {
        AppDelegate *_rootDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        _userModel = _rootDelegate.userModel;
    }
    return _userModel;
}

@end
